<?php

/**
 * uniBoard ACP Edit Mass Mail
 *
 * Light and fast forum solution for web 2.0-like communities.
 *
 * @package		uniBoard
 * @author		Rafał Pitoń
 * @license		http://www.gnu.org/licenses/gpl-3.0.html
 * @since		Version 0.1
 * 
 * ------------------------------------------------------------------------
 * 
 * $Date: 2010-09-20 21:23:27 +0000 (Mon, 20 Sep 2010) $
 * $Revision: 64 $
 * $Author: rafio.xudb@gmail.com $
 */

// ------------------------------------------------------------------------

class action_edit_mass_mail{
	
	/**
	 * Main class pointer
	 *
	 * @var object
	 */
	
	private $appCore		= NULL;
	
	/**
	 * Avaiable Groups
	 *
	 * @var array
	 */
	
	private $groups			= array();
	
	/**
	 * Our api
	 *
	 * @var object
	 */
	
	private $api			= NULL;
	
	// ------------------------------------------------------------------------

	/**
	 * Initialise Action
	 *
	 * @param object $appCore
	 */
	
	function __construct( $appCore){
		
		// Store appCore pointer
		$this -> appCore = $appCore;
		
		// Get Groups
		$this -> groups = $appCore -> users -> getUserGroups();
		
		// Create API
		$this -> api = new api_mass_mail( $appCore, $appCore -> _REQ('mail'));
		
		// Api found?
		if ( !$this -> api -> data_loaded)
		{
			// Draw Error
			$appCore -> output -> error( $appCore -> lang['acp_mail_not_found'], $appCore -> lang['acp_mass_mail']);
			
			// Jump back to Tasks List
			new action_mass_mail( $appCore);
		}
		else if ( $appCore -> _REQ('do') == 'edit')
		{
			// Update?
			if ( $appCore -> _POST( 'update') == 1)
			{
				// Fill Api
				$this -> api -> setMailGroups( $appCore -> listInput( 'mail_groups'));
				$this -> api -> setMailEveryone( $appCore -> _POST( 'mail_everyone'));
				$this -> api -> setMailSubject( $appCore -> _POST( 'mail_subject'));
				$this -> api -> setMailContent( $appCore -> _POST( 'mail_content'));
				
				// Api All-rite?
				if ( $this -> api -> error)
				{
					// Draw Errors
					$appCore -> output -> error( join( '<br />', $this -> api -> errors), $appCore -> lang['acp_edit_mail_form']);
					
					// Repeat Form
					$this -> drawForm();
				}
				else
				{
					// Save Api
					$this -> api -> saveData();
					
					// Set Log
					$appCore -> logAdmin( 'log_admin_edited_mass_mail', htmlspecialchars( $appCore -> _POST( 'mail_subject')));
					
					// Show message
					$appCore -> output -> message( $appCore -> lang['acp_mail_edited'], htmlspecialchars( $this -> api -> mail_subject));
					
					// Hop back to list
					new action_mass_mail( $appCore);
				}
			}
			else
			{
				// Draw Form
				$this -> drawForm();
			}
		}
		else if ( $appCore -> _REQ('do') == 'delete')
		{
			// Delete Api
			$this -> api -> deleteData();
			
			// Set Log
			$appCore -> logAdmin( 'log_admin_deleted_mass_mail', htmlspecialchars( $this -> api -> mail_subject));
			
			// Show message
			$appCore -> output -> message( $appCore -> lang['acp_mail_deleted'], htmlspecialchars( $this -> api -> mail_subject));
			
			// Hop back to list
			new action_mass_mail( $appCore);
		}
		else
		{
			// Draw Error
			$appCore -> output -> error( $appCore -> lang['wrong_action_code'], $appCore -> lang['acp_mass_mail']);
			
			// Jump back to Mails List
			new action_mass_mail( $appCore);
		}
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Draws Form
	 *
	 */
	
	function drawForm(){
	
		// Load fields templates
		$this -> appCore -> templates['form_inputs'] = new tpl_form_inputs( $this -> appCore);
				
		// Cache output
		ob_start();
				
		// Open Form
		echo $this -> appCore -> output -> openForm( $this -> appCore -> adminLink( 'act=edit_mass_mail&do=edit&mail=' . $this -> api -> mail_id));
		echo $this -> appCore -> drawHidden( 'update', 1);
		echo $this -> appCore -> output -> openFormBlock( $this -> appCore -> lang['acp_mail_send_settings']);
		
		// Draw fields
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['acp_mail_form_groups'], $this-> appCore -> drawMultiList( 'mail_groups', $this -> groups, $this -> api -> mail_groups));
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['acp_mail_form_everyone'], $this-> appCore -> drawYesNo( 'mail_everyone', $this -> api -> mail_everyone), $this -> appCore -> lang['acp_mail_form_everyone_expl']);
				
		// New block
		echo $this -> appCore -> output -> closeFormBlock();
		echo $this -> appCore -> output -> openFormBlock( $this -> appCore -> lang['acp_mail_form_content']);
		
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['acp_mail_form_subject'], $this-> appCore -> drawTextInput( 'mail_subject', $this -> api -> mail_subject));
		echo $this -> appCore -> output -> tableRow( $this -> appCore -> drawBigTextArea( 'mail_content', $this -> api -> mail_content));
				
		// Close Form
		echo $this -> appCore -> output -> submitButton( $this -> appCore -> lang['save_changes']);
		echo $this -> appCore -> output -> closeFormBlock();
		echo $this -> appCore -> output -> closeForm();
		
		// Draw output
		$this -> appCore -> output -> draw( $this -> appCore -> lang['acp_edit_mail_form'], array(
			$this -> appCore -> adminLink( 'act=mass_mail') => $this -> appCore -> lang['acp_mass_mail'],
			'' => htmlspecialchars( $this -> api -> mail_subject)
		), ob_get_clean());
			
	}

}